package com.example.diary

import DiaryDbHelper
import android.app.Activity
import android.app.AlertDialog
import android.content.ContentValues
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.example.R

class DiaryAddFragment : Fragment() {

    private lateinit var etTitle: EditText
    private lateinit var etdescription: EditText

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_add, container, false)
        etTitle = view.findViewById(R.id.et_title)
        etdescription = view.findViewById(R.id.et_description)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val btnSure: Button = view.findViewById(R.id.btn_sure)
        val btnCancel: Button = view.findViewById(R.id.btn_cancel)

        btnSure.setOnClickListener {
            val title = etTitle.text.toString()
            val content = etdescription.text.toString()
            if (title.isNotEmpty() && content.isNotEmpty()) {
                addDiary(requireActivity(), title, content)
            } else {
                if (title.isEmpty())
                    Toast.makeText(requireContext(), "标题不能为空", Toast.LENGTH_SHORT).show()

                if (content.isEmpty())
                    Toast.makeText(requireContext(), "内容不能为空", Toast.LENGTH_SHORT).show()
            }
        }

        btnCancel.setOnClickListener {
            val title = etTitle.text.toString()
            val content = etdescription.text.toString()
            if (title.isNotEmpty() || content.isNotEmpty()) {
                cancelAdd(requireActivity())
            } else {
                requireActivity().finish()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        val prefs = requireActivity().getPreferences(Context.MODE_PRIVATE)
        val lastDailyTitle = prefs.getString("last_daily_title", "")
        val lastDailyContent = prefs.getString("last_daily_content", "")

        etTitle.setText(lastDailyTitle)
        etdescription.setText(lastDailyContent)
    }

    private fun addDiary(activity: Activity, title: String, content: String) {
        AlertDialog.Builder(activity).apply {
            setTitle("添加日记")
            setMessage("您确认添加当前日记吗")
            setCancelable(false)
            setPositiveButton("确定") { _, _ ->
                val dbHelper = DiaryDbHelper(activity, "diary.db", 1)
                val db = dbHelper.writableDatabase
                val values = ContentValues().apply {
                    put("title", title)
                    put("description", content)
                }
                db.insert("diary", null, values)
                Toast.makeText(activity, "日记已添加", Toast.LENGTH_SHORT).show()

                etTitle.setText("") // Clear title field
                etdescription.setText("") // Clear description field

            }
            setNegativeButton("取消") { _, _ -> }
        }.show()

    }

    private fun cancelAdd(activity: Activity) {
        AlertDialog.Builder(activity).apply {
            setTitle("取消添加日记")
            setMessage("您确定放弃添加该日记吗？已编辑的内容不会保存")
            setCancelable(false)
            setPositiveButton("确定") { _, _ ->
                etTitle.setText("") // Clear title field
                etdescription.setText("") // Clear description field
            }
            setNegativeButton("取消") { _, _ -> }
        }.show()
    }
}

